<template>
  <view>
    <!--    <view class="error-message" v-if="hasGoods">
      暂时没有商品！
    </view> -->
    <view class="goods-list">
      <view v-for="(goods,index) in goodsList" :key="goods.goods_id" @click="gotoDetail(goods)">
        <my-goods :goods="goods"></my-goods>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        params: {
          cid: '',
          query: '',
          pagenum: 1,
          pagesize: 10
        },
        goodsList: [],
        total: 0,
        isLoading: false
        // hasGoods:true
      };
    },
    onLoad(options) {
      console.log(options)
      // 当数值参与逻辑或运算时，结果为true，会返回第一个为真的值；如果结果为false，会返回第二个为假的值
      //防止options.cid和options.query为undefined
      this.params.cid = options.cid || '';
      this.params.query = options.query || '';
      this.getGoodsList()
    },
    onPullDownRefresh() {
      this.params.pagenum = 1;
      this.total = 0;
      this.goodsList = [];
      this.isLoading = false;

      this.getGoodsList(() => uni.stopPullDownRefresh())
    },
    onReachBottom() {
      //说明没有下一页数据了
      if (this.params.pagenum * this.params.pagesize >= this.total) {
        return uni.$showMsg('没有更多商品了...');
      }

      if (this.isLoading) {
        return;
      }

      this.params.pagenum++
      this.getGoodsList()
    },
    methods: {
      async getGoodsList(callback) {
        //打开节流阀
        this.isLoading = true
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/search', this.params)
        if (res.meta.status !== 200) {
          return uni.$showMsg()
        }
        // console.log(res)

        this.goodsList = [...this.goodsList, ...res.message.goods]
        if (this.params.pagenum === 1) {
          this.total = res.message.total;
        }
        //关闭节流阀
        this.isLoading = false
        //如果callback不为空，那么执行callback()
        callback && callback()
      },
      gotoDetail(goods) {
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id=' + goods.goods_id
        })
      }
    }


  }
</script>

<style lang="scss">
  .goods-list {}
</style>
